<template>
  <div class="register">
    <div class="register_ct">
      <h4>新用户注册</h4>
      <p>很高兴您将成为商城的会员（注册只需一步）</p>
      <my-input
        v-model="mobile"
        :class="reMobile == 0 ? '' : reMobile == 1 ? 'success' : 'error'"
        placeholder="手机号码"
        @blur="mobileFtn"
      >
        <div class="icon">
          <b v-if="reMobile == 2" style="color: red">必填</b>
          <i class="iconfont icon-shouji"></i>
        </div>
      </my-input>
      <my-input
        v-model="pwd"
        :type="showPwd ? 'text' : 'password'"
        placeholder="请输入密码"
        :class="rePwd == 0 ? '' : rePwd == 1 ? 'success' : 'error'"
        @blur="pwdFtn"
      >
        <div class="icon" @click="showPwd = !showPwd">
          <i
            :class="['iconfont', showPwd ? 'icon-zhengyan' : 'icon-biyan']"
          ></i>
        </div>
      </my-input>
      <my-input
        v-model="pwdagain"
        :type="showPwdagain ? 'text' : 'password'"
        placeholder="请输入密码"
        :class="rePwdagain == 0 ? '' : rePwdagain == 1 ? 'success' : 'error'"
        @blur="pwdFtnagain"
      >
        <div class="icon" @click="showPwdagain = !showPwdagain">
          <i
            :class="['iconfont', showPwdagain ? 'icon-zhengyan' : 'icon-biyan']"
          ></i>
        </div>
      </my-input>
      <my-input
        v-model="nick"
        placeholder="请输入用户名"
        :class="renick == 0 ? '' : renick == 1 ? 'success' : 'error'"
        @blur="nickFtn"
      >
        <div class="icon">
          <i class="iconfont icon-gerentouxiang"></i>
        </div>
      </my-input>
      <my-input
        v-model="picCode"
        placeholder="请输入图形验证码"
        :class="rePic == 0 ? '' : rePic == 1 ? 'success' : 'error'"
        @blur="picFtn"
      >
        <img :src="picSrc" alt="" @click="codeFtn" />
      </my-input>
      <my-input v-model="cityStr" @click="show=true"></my-input>
      <my-input
        v-model="code"
        placeholder="请输入验证码">
         <span style="width:100px" @click="getCodeFtn">{{codeText}}</span>
      </my-input>
      <button>立即注册</button>
      <router-link to="/login">已有账号？立即登录</router-link>
    </div>
    <van-action-sheet v-model:show="show" title=" ">
      <van-area :area-list="areaList" :columns-num="2" @confirm="confirm"/>
    </van-action-sheet>
  </div>
</template>

<script>
import { areaList } from '@vant/area-data';
import { reactive, toRefs } from "vue";
import myInput from "../../components/myInput.vue";
import { useRouter } from "vue-router";
import { getCode } from '../../api/second/second';
export default {
  components: { myInput },
  setup() {
    const router = useRouter();
    const data = reactive({
      mobile: "", // 手机号
      pwd: "", // 密码
      reMobile: 0, // 0 就代表，没有填写过，1代表成功，2代表失败
      rePwd: 0, // 密码的验证
      nick: "", // 用户名
      showPwd: false, // 控制密码的图标
      pwdagain: "", // 确认密码
      showPwdagain: false, // 控制再次确认密码的图标
      rePwdagain: 0, // 在此确认密码的验证
      renick: 0, // 用户名的验证
      picCode: "", // 图形验证码
      rePic: 0, // 图形验证码的验证
      picSrc: "https://api.it120.cc/small4/verification/pic/get?key=1234",
      city: [],
      show: false,
      areaList: areaList,
      cityStr:'',
      code: '',
      codeText: '获取验证码',
      time: null
    });
    const mobileFtn = () => {
      // 手机号的验证
      let re = /^1[3-9][0-9]{9}$/;
      data.reMobile = re.test(data.mobile) ? 1 : 2;
    };
    const pwdFtn = () => {
      // 密码的验证
      data.rePwd = data.pwd ? 1 : 2;
    };
    const pwdFtnagain = () => {
      // 再次确认密码的验证
      data.rePwdagain = data.pwd == data.pwdagain ? 1 : 2;
    };

    const nickFtn = () => {
      // 用户名的验证
      data.renick = data.nick ? 1 : 2;
    };
    const picFtn = () => {
      // 图形验证码的验证
      data.rePic = data.picCode ? 1 : 2;
    };
    const codeFtn = () => {
      data.picSrc = `https://api.it120.cc/small4/verification/pic/get?key=${new Date().getTime()}`;
    };
    const confirm = (val) => {
       data.city = val;
       data.cityStr = `${val[0].name} ${val[1].name}`;
       data.show = false;
    }
    const getCodeFtn = () => {
       let num = 60;
       if (data.time){
           return;
       }
       getCode({mobile:data.mobile,key:new Date().getTime(),picCode:data.picCode}).then(res => {
           console.log(res);
       })

       data.time = setInterval(()=>{
          if (num<=0){
            clearInterval(data.time);
            data.time = null;
            data.codeText = '获取验证码';
          } else {
              num--;
              data.codeText = `${num}秒后再试`;
          }
       },1000)
    }
    return {
      ...toRefs(data),
      mobileFtn,
      pwdFtn,
      pwdFtnagain,
      nickFtn,
      picFtn,
      codeFtn,
      confirm,
      getCodeFtn
    };
  },
};
</script>
<style lang="scss" scoped>
.register {
  padding: 40px;
  .register_ct {
    padding: 0 60px;
    border: 2px solid #f5f5f5;
    text-align: center;
    h4 {
      margin-top: 60px;
      color: #666;
    }
    p {
      margin-top: 40px;
      color: #909090;
      font-size: 32px;
    }
    button {
      color: #fff;
      font-size: 30px;
      width: 420px;
      height: 100px;
      border: none;
      border-radius: 10px;
      margin-top: 40px;
      background: linear-gradient(135deg, #e570e7 0%, #79f1fc 100%);
    }
    a {
      display: block;
      font-size: 32px;
      color: #549ff9;
      margin: 40px 0 300px;
    }
    .error {
      border-color: rgb(225, 44, 30);
      background: rgb(255, 233, 228);
    }
    .success {
      border-color: rgb(56, 219, 36);
      background: rgb(225, 255, 223);
    }
  }
}
.icon {
  font-size: 24px;
  .iconfont {
    font-size: 36px;
    margin-left: 10px;
  }
}
img {
  width: 260px;
}
</style>